package com.xxh.upload.service.impl;


import cn.hutool.core.date.DateUtil;
import com.jlefebure.spring.boot.minio.MinioConfigurationProperties;
import com.jlefebure.spring.boot.minio.MinioException;
import com.jlefebure.spring.boot.minio.MinioService;
import com.xxh.upload.service.upLoadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
@Slf4j
public class upLoadServiceImpl implements upLoadService {


    String bucketName = "xxvideo";

    @Autowired
    MinioService minioService;

    @Autowired
    private MinioConfigurationProperties configurationProperties;

    @Override
    public String upload(MultipartFile file) {
        String originName = file.getOriginalFilename();
        //当前系统时间戳作为文件前缀
        // yyyy-MM-dd HH:mm:ss
        String prefix = String.valueOf(System.currentTimeMillis());
        String objectName = prefix + originName;
        Path path = Paths.get(objectName);
        String url = configurationProperties.getUrl() + configurationProperties.getBucket() + "/"+path;
        try {
            minioService.upload(path, file.getInputStream(), file.getContentType());
//            return "http://123.57.11.136:9000/" + bucketName + "/" + objectName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (MinioException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean deleteByUrl(String url) {

        String[] split = url.split("/");
        String objsetName = split[split.length - 1];
//        return fileService.delete(objsetName);
        return false;
    }

    @Override
    public boolean deleteByName(String objectName) {
//        return fileService.delete(objectName);
        return false;
    }


}
